﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using CourseSystem.Common.Chat;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using CourseSystem.Core.Enum;

namespace CourseSystem.Web.Controllers
{
    public class AjaxController : Controller
    {
        protected new JsonResult Json(object data)
        {
            return Json(data, "text/html", JsonRequestBehavior.AllowGet);
        }

        [UserAuth(IsLimt = true, UserType = UserTypeEnum.Student)]
        public ActionResult GetExamStatus(int id)
        {
            var info = ExamStatusBLL.Get(id, UserHelper.LoginUser.Id);
            return Json(info);
        }

        public ActionResult GetCollege()
        {
            return Json(StudentHelper.GetCollege(true));
        }

        public ActionResult GetSubject(int id)
        {
            return Json(StudentHelper.GetSubject(id, true));
        }

        public ActionResult GetClass(int id)
        {
            return Json(StudentHelper.GetClass(id, true));
        }

        public ActionResult GetDepart(int id)
        {
            return Json(StudentHelper.GetDepartment(id, true));
        }

        public ActionResult GetGradeYear()
        {
            return Json(StudentHelper.GetGradeYear(true));
        }

        public ActionResult GetTeacher(int? id)
        {
            return Json(TeacherHelper.GetTeacher(!id.HasValue));
        }

        public ActionResult GetExamLib()
        {
            return Json(ExamLibHelper.GetLib(true));
        }

        public ActionResult GetChapter()
        {
            return Json(ChapterHelper.GetChapter(false));
        }

        public ActionResult GetChapterDef()
        {
            return Json(ChapterHelper.GetChapter(true));
        }

        public ActionResult GetSection(int id)
        {
            return Json(SectionHelper.GetSection(id, false));
        }

        public ActionResult AddChatMessage(string message)
        {
            if (string.IsNullOrEmpty(message))
                return Content("");
            ChatHelper.AddMessage(message);
            return Content("发送成功.");
        }

        public ActionResult GetChatMessage()
        {
            var infoList = ChatHelper.ChatList;
            var sb = new StringBuilder();
            foreach (var item in infoList)
            {
                sb.Append("<div id='Content'><p>" + item.SendTime.ToString("yyyy-MM-dd HH:mm") + "</p><p>" + item.Name + "说:" +
                          item.Message + "</p></div>");
            }
            return Content(sb.ToString());
        }

        public ActionResult SelectLibItem(int id, int eid)
        {
            var info = ExaminationBLL.Get(eid);
            if (info == null)
                return Json(new { error = 1 });

            var qInfo = QuestionLibBLL.Get(id);
            if (qInfo == null)
                return Json(new { error = 1 });
            var existInfo = info.ExaminationItemInfo.FirstOrDefault(o => o.Eid == eid && o.Qid == id);
            if (existInfo != null)
            {
                if (!ExaminationItemBLL.Del(existInfo))
                    return Json(new { error = 1 });
                ExaminationBLL.UpdateExaminationScore(eid);
                //如果是考试类型，检测是否需要取消发布
                if (info.Type == 4)
                {
                    info = ExaminationBLL.Get(eid);
                    if (info == null)
                        return Json(new { error = 1 });
                    if (info.Score != 100)
                    {
                        info.Status = 0;
                        ExaminationBLL.Edit(info);
                    }
                }
                return Json(new { error = 0, now = 0 });
            }
            var add = new ExaminationItemInfo { Eid = eid, Qid = id };
            if (!ExaminationItemBLL.Add(add))
            {
                return Json(new { error = 1 });
            }
            qInfo.UseTimes++;
            QuestionLibBLL.Edit(qInfo);
            //if (add.ExaminationInfo.Type == 4)
            ExaminationBLL.UpdateExaminationScore(add.Eid);
            //如果是考试类型，检测是否需要取消发布
            if (add.ExaminationInfo.Type == 4)
            {
                info = ExaminationBLL.Get(eid);
                if (info == null)
                    return Json(new { error = 1 });
                if (info.Score != 100)
                {
                    info.Status = 0;
                    ExaminationBLL.Edit(info);
                }
            }
            return Json(new { error = 0, now = 1 });
        }
    }
}
